﻿using ERPEF.Model;
using ERPIService;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;
using S4Project;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ERPService
{
    public class pur_Purchase_OrderService : pur_Purchase_OrderIService
    {
        private readonly MyDbContext MyDbContext;
        public pur_Purchase_OrderService(MyDbContext myDb)
        {
            this.MyDbContext = myDb;
        }

        //主添加
        public async Task<int> OrderAdd(pur_Purchase_Order PurOrder)
        {
            await MyDbContext.pur_Purchase_Order.AddAsync(PurOrder);
            return await MyDbContext.SaveChangesAsync();
        }

        //主删除
        public async Task<int> OrderDelete(int ord_id)
        {
            var del = MyDbContext.pur_Purchase_Order.Find(ord_id);
            if (del!=null)
            {
                del.IsDeleted = true;
                await MyDbContext.SaveChangesAsync();
                return 1;
            }
            return 0;
        }

        //主查询
        public async Task<List<pur_Purchase_Order>> OrderQuery()
        {
            return await MyDbContext.pur_Purchase_Order.Where(c => c.IsDeleted == false).ToListAsync();
        }
        //查询供货商
        public async Task<List<bs_vendor>> VenQuery()
        {
            return await MyDbContext.bs_Vendor.Where(w => w.IsDeleted == false).ToListAsync();
        }

        //主修改
        public async Task<int> OrderUpdate(pur_Purchase_Order PurOrder)
        {
            MyDbContext.pur_Purchase_Order.Update(PurOrder);
            return await MyDbContext.SaveChangesAsync();
        }



        //从添加
        public async Task<int> FromAdd(pur_Purchase_From PurFrom)
        {
            await MyDbContext.pur_Purchase_From.AddAsync(PurFrom);
            return await MyDbContext.SaveChangesAsync();
        }

        //从修改
        public async Task<int> FromUpdate(pur_Purchase_From PurFrom)
        {
            MyDbContext.pur_Purchase_From.Update(PurFrom);
            return await MyDbContext.SaveChangesAsync();
        }

        //从删除
        public async Task<int> FromDelete(int fro_id)
        {
            var del = MyDbContext.pur_Purchase_From.Find(fro_id);
            if (del != null)
            {
                del.IsDeleted = true;
                await MyDbContext.SaveChangesAsync();
                return 1;
            }
            return 0;
        }
    }
}
